<template>
	<view class="">
		<view class="nav_top-bg">
			<u-navbar title="身份信息">
				<view class="u-nav-slot" slot="left">
					<u-icon name="arrow-left" @click="go_black" size="19"></u-icon>
					<u-line direction="column" :hairline="false" length="16" margin="0 10px"></u-line>
					<u-icon name="home" @click="goHome" size="20"></u-icon>
				</view>
			</u-navbar>
		</view>
		<view class="">
			<view class="avatar">
				<text class="touxtxt">行业类型</text>
				<view class="righttx d-flex align-items" @click="show_type = true">
					<view class="top-txt_xz">{{type_name}}</view>
					<image class="qm" src="../../static/index/more.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">企业名称</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="请输入企业名称" v-model="compony_name">
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">联系人</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="请输入姓名" v-model="name">
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">电话</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="请输入电话" v-model="phone">
				</view>
			</view>
			
			<view class="avatar">
				<text class="touxtxt">地区</text>
				<view class="righttx d-flex align-items" @tap="chooseCity()">
					<view :class="adress == '请选择' ? 'top-txt' : 'top-txt_xz'">{{adress}}</view>
					<image class="qm" src="../../static/index/more.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="upload_zhizhao">
				<text class="zhizhao_tit">营业执照</text>
				<view class="righttx">
					<view class="adress_suozai">
						<u-upload width="127rpx" height="127rpx" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="4">
							<image :src="src" class="shangchuanimg" v-if="avatar"></image>
							<image src="../static/upload.png" class="shangchuanimg" v-else></image>
						</u-upload>
					</view>
				</view>
			</view>
			<view class="upload_zhizhao">
				<text class="zhizhao_tit">主页背景图</text>
				<view class="righttx">
					<view class="adress_suozai">
						<u-upload width="127rpx" height="127rpx" :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
							:maxCount="1">
							<image :src="src" class="shangchuanimg" v-if="avatar"></image>
							<image src="../static/upload.png" class="shangchuanimg" v-else></image>
						</u-upload>
					</view>
				</view>
			</view>
			<view class="w-690 margin-top-30">
				<view class="jieshao_t">介绍</view>
				<view class="">
					<textarea name="" class="xinxi_content" v-model="jieshao" placeholder="请输入个人/公司介绍" id=""
						cols="30" rows="10"></textarea>
				</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-foot">
				<u-button text="确定提交" @click="submit" color="#FF6400"></u-button>
			</view>
		</view>
		<!-- 行业类型 -->
		<u-picker :show="show_type" @confirm="confirm_leixing" @cancel="show_type = false" :columns="columns_type"
			keyName="label"></u-picker>
		<!-- 选择地区 -->
		<u-picker :show="showcity" ref="uPicker" @cancel="showcity = false" :columns="columns_city" @confirm="confirm_city"
			@change="changeHandler" keyName="t_name"></u-picker>
	</view>
</template>

<script>
	import {
		tree
	} from '@/api/citys.js'
	import {
		upFile
	} from '@/utils/url.js'
	import {
		saveXtUserInfo,
		getGroupUserinfo
	} from '@/api/user.js'
	export default {
		data() {
			return {
				compony_name: '',
				name:'',
				phone:'',
				adress: '请选择',
				cityname: '',
				shiname: '',
				quname: '',
				shengid: '',
				shiid: '',
				quid:'',
				user: {},
				jieshao:'',
				avatar: '',
				//域名头像
				full_avatar: '',
				fileList1:[],
				fileList3:[],
				avatar: '',
				src: require('../static/upload.png'),
				showcity:false,
				columns_city:[],
				group_id:'',
				show_type: false,
				type_id: '', //行业类型id
				type_name: '请选择',
				columns_type: [
					[{
						id:1,
						label:'建房材料商'
					},{
						id:2,
						label:'装修材料商'
					}]
				], //行业类型数组
			}
		},
		async onLoad(option) {
			this.group_id = option.id
			await this.getProvinces()
			this.get_juse_info()
		},
		methods: {
			// 获取角色信息
			get_juse_info() {
				var data = {
					group_id: this.group_id
				}
				getGroupUserinfo(data).then(res => {
					console.log("获取身份信息有没有填写", res);
					if (res.code == 1) {
						let userData = res.data.user_groupinfo
						this.compony_name = userData.company_name
						this.name = userData.company_contact
						this.phone = userData.company_phone
						this.adress = userData.company_provincename + '/' + userData.company_cityname + '/' + userData.company_areaname
						this.shengid = userData.company_province
						this.shiid = userData.company_city
						this.quid = userData.company_area
						this.jieshao = userData.instr
						this.fileList3 = [{
							url: userData.company_bg_image_url,
							url_noyuming: userData.company_bg_image,
						}]
						if(userData.company_zhizhao) {
							let c = []
							let d = userData.company_zhizhao.split(',')
							for (let s in userData.company_zhizhao_url) {
								console.log("营业执照",s);
								c.push({
									url:userData.company_zhizhao_url[s],
									url_noyuming:d[s]
								})
							}
							this.fileList1 = c
						}
						
					}
				})
			},
			go_black() {
				uni.navigateBack({
					delta: 1
				})
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			radioChange(n) {
				console.log('radioChange', n);
			},
			chooseCity() {
				console.log("eeeee", this.columns_city);
				this.showcity = true
			},
			// 省市区
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values,
					index,
					indexs,
					picker = this.$refs.uPicker
				} = e
				if (columnIndex === 0) {
					picker.setColumnValues(1, this.columns_city[0][index].children)
					picker.setColumnValues(2, this.columns_city[0][index].children[0].children)
				}
				if (columnIndex === 1) {
					console.log(e, indexs);
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(2, this.columns_city[0][indexs[0]].children[indexs[1]].children)
				}
				console.log("columnIndex", columnIndex);
			},
			confirm_city(e) {
				this.shengid = e.value[0].id
				this.shiid = e.value[1].id
				this.quid = e.value[2].id
				this.cityname = e.value[0].name
				this.shiname = e.value[1].name
				this.quname = e.value[2].name
				this.adress = this.cityname + '/' + this.shiname + '/' + this.quname
				this.showcity = false
			},
			// 材料商类型选择
			confirm_leixing(e){
				console.log("材料商选择",e);
				this.type_name = e.value[0].label
				this.type_id = e.value[0].id
				this.show_type = false
			},
			// 获取省市县接口数据
			async getProvinces() {
				var data = {
					pid: 0
				}
				await tree(data).then(res => {
					console.log("选择了第几个", res);
					if (res.code == 1) {
						this.columns_city[0] = res.data
						this.columns_city[1] = res.data[0].children
						this.columns_city[2] = res.data[0].children[0].children
					}
			
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				var url = event.file[0].url
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				console.log("00000000000000", lists);
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						fullurl: result.fullurl,
						url_noyuming: result.url
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this
				console.log("url有没有值", url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: upFile,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							console.log("s111111111111111______", res);
							var data = JSON.parse(res.data);
							resolve(data.data)
						}
					});
				})
			},
			submit(){
				// 营业执照
				let compony_arr = []
				for (let s of this.fileList1) {
					compony_arr.push(s.url_noyuming)
				}
				var data = {
					hangye_id:this.type_id,
					group_id: this.group_id,
					company_name:this.compony_name,
					company_contact:this.name,
					company_phone:this.phone,
					company_province: this.shengid,
					company_city:this.shiid,
					company_area:this.quid,
					instr: this.jieshao,
					company_bg_image:this.fileList3.length ? this.fileList3[0].url_noyuming : '', 
					company_zhizhao: compony_arr.join(','),
				}
				saveXtUserInfo(data).then(res => {
					console.log("保存数据成功",res);
					if(res.code == 1){
						uni.showToast({
							title:'已提交审核，请耐心等待!',
							icon:'none'
						})
						setTimeout(() => {
							uni.navigateBack({
								delta:1
							})
						},500)
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
		}
	}
</script>

<style>
	page {
		padding-bottom: 150rpx;
	}
	.jieshao_t{
		font-size: 28rpx;
		color: #333;
	}
	.xinxi_content {
		width: 650rpx;
		height: 160rpx;
		background-color: #F5F5F5;
		border-radius: 12rpx;
		margin: auto;
		font-size: 24rpx;
		color: #000000;
		margin-top: 30rpx;
		padding: 20rpx;
	}
	.btn{
		width: 750rpx;
		position: fixed;
		bottom: 40rpx;
		z-index: 10;
	}
	.btn-foot{
		width: 690rpx;
		margin: auto;
	}
	.shangchuanimg {
		width: 127rpx;
		height: 127rpx;
		border: 1px solid #eee;
		display: block;
		margin: auto;
		overflow: hidden;
	}
	.top-txt_xz {
		font-size: 30rpx;
		color: #333;
	}
	.top-txt{
		font-size: 30rpx;
		color: #999999;
	}
	.youcejt {
		width: 12rpx;
		display: block;
		margin-left: 20rpx;
	}

	.ipt {
		font-size: 30rpx;
		font-weight: normal;
		text-align: right;
		color: #333333;
	}

	.qm {
		width: 12rpx;
		display: block;
		margin-left: 10rpx;
	}

	.righttx {
		display: flex;
		align-items: center;
	}

	.touxtxt {
		font-size: 30rpx;
		font-weight: normal;
		display: block;
		color: #333333;
	}
	.zhizhao_tit{
		font-size: 30rpx;
		font-weight: normal;
		display: block;
		color: #333333;
		margin: 30rpx 0;
	}
	.upload_zhizhao{
		width: 690rpx;
		margin: auto;
	}

	.avatar {
		width: 690rpx;
		border-bottom: 1px solid #f5f5f5;
		height: 110rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: auto;
	}
</style>